<?php
namespace Ucmaim\Model;
use Think\Model;


class GoodsModel extends Model{
    // 前台查询商品列表
    public function get($category, $page = 0, $pageSize = 0, &$show_page){
        $category_where = '';
        if( $category !== 0 ){
            $category_where = " AND (C.gc_relation LIKE '%$category%')";
        }

        if(  $page > 0 && $pageSize > 0){
            $this->alias('A');
            $where = "A.goods_status = 1 AND (A.goods_storage - A.goods_freeze > 0)" . $category_where;
            $this->where($where);
            $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
            $this->join('uc_goods_category C on A.gc_id = C.gc_id');
            $this->order('A.goods_id desc');
            $total = $this->count();
            $show_page = tpl_page($total, $pageSize);
            $this->page($page, $pageSize);
        }

        $this->alias('A');
        $field = ['A.*', '(A.goods_storage - A.goods_freeze) as show_storage', 'B.type_id', 'B.type_name'];
        $where = "A.goods_status = 1 AND (A.goods_storage - A.goods_freeze > 0)" . $category_where;

        $this->where($where);
        $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
        $this->join('uc_goods_category C on A.gc_id = C.gc_id');
        $this->order('A.goods_id desc');
        $this->field($field);
        $data = $this->select();

        foreach($data as $k => $v){
            $M = M('goods_category');
            $gc_id = $v['gc_id'];
            $map['gc_id'] = $gc_id;
            $result = $M->field('gc_relation, gc_id, gc_name')->where($map)->find();
            $relation = $result['gc_relation'];
            $self = array(
                'gc_id'   => $result['gc_id'],
                'gc_name' => $result['gc_name']
            );
            if( $relation ){
                $map = [];
                $map['gc_id'] = array('IN', extraCharFilter($relation));
                $result = $M->field('gc_id, gc_name')->where($map)->select();
                $result[] = $self;
                $data[$k]['goods_relation'] = $result;
            }
        }

        return $data;
    }

    // 根据用户ID查询卖家商品列表
    public function getSellerGoods($uid, $condition = [], $page = 0, $pageSize = 0, &$show_page){
        if(  $page > 0 && $pageSize > 0){
            $where = "A.member_id = $uid";
            $this->where($where);
            $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
            $this->join('uc_goods_category C on A.gc_id = C.gc_id');
            $this->order('A.goods_id desc');
            $total = $this->count();
            $show_page = tpl_page($total, $pageSize);
            $this->page($page, $pageSize);
        }

        $this->alias('A');
        $field = ['A.*', '(A.goods_storage - A.goods_freeze) as show_storage', 'B.type_id', 'B.type_name'];
        $where = "A.member_id = $uid";
        $this->where($where);
        $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
        $this->join('uc_goods_category C on A.gc_id = C.gc_id');
        $this->order('A.goods_id desc');
        $this->field($field);
        $this->where($condition);
        $data = $this->select();

        foreach($data as $k => $v){
            $M = M('goods_category');
            $gc_id = $v['gc_id'];
            $map['gc_id'] = $gc_id;
            $result = $M->field('gc_relation, gc_id, gc_name')->where($map)->find();
            $relation = $result['gc_relation'];
            $self = array(
                'gc_id'   => $result['gc_id'],
                'gc_name' => $result['gc_name']
            );
            if( $relation ){
                $map = [];
                $map['gc_id'] = array('IN', extraCharFilter($relation));
                $result = $M->field('gc_id, gc_name')->where($map)->select();
                $result[] = $self;
                $data[$k]['goods_relation'] = $result;
            }
        }

        return $data;
    }

    //下架商品
    public function soldOut($uid,$gid){
        $where = "member_id = $uid And goods_id=$gid";
        $this->where($where);
        $data['goods_status']=-1;
        $data = $this->save($data);
        return $data;
    }


    //由id查询商品信息
    public function getGoodsInfo($id, $status = 1){
        $field = ['A.*', '(A.goods_storage - A.goods_freeze) as show_storage','(A.goods_price / A.goods_amount) as show_price', 'B.type_name', 'C.gc_name','C.gc_relation'];

        if( $status !== false ){
            $map['A.goods_status'] = $status;
        }
        $map['A.goods_id'] = $id;
        $map['A.goods_storage'] = array('exp', ' > A.goods_freeze');
        $this->alias('A');
        $this->where($map);
        $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
        $this->join('uc_goods_category C on A.gc_id = C.gc_id');
        $this->order('A.goods_id desc');
        $this->field($field);
        $data = $this->find();

        if( !empty($data['gc_relation']) ){
            $data = D('GoodsCategory')->getRelation($data);
        }
        return $data;
    }

    //更改商品库存
    public function updateGoodsStorage($type = '', $num, $id){
        $sign = '';
        $num  = (int)$num;
        switch($type) {
            case 'Add':
                $sign = '+';
                break;
            case 'Cancel':
                $sign = '-';
                break;
            case 'Received':
                $sign = '-';
                $data['goods_storage'] = array('exp', 'goods_storage -'. $num);
                break;
            Default:
                $this->getError();
                break;
        }
        $data['goods_freeze'] = array('exp', 'goods_freeze'. $sign. $num);
        $map['goods_id'] = (int)$id;
        $result = $this->where($map)->save($data);

        return $result;
    }

    /**
     * 更改商品状态
     *  ->  0 待审核
     *  ->  1 审核通过
     *  -> -1 已下架
     *  -> -3 商品已删除
     *  -> -2 审核不通过
     *
     * @param $id
     * @param $status
     * @return bool
     */
    public function updateGoodsStatus($id,$status){
        $data['goods_status']  = (int)$status;
        $map['goods_id'] = (int)$id;

        $result = $this->where($map)->save($data);
        return $result;

    }

    //查询全部商品
    public function getAll($page = 0, $pageSize = 0, &$show_page, $map,$order){
        if(  $page > 0 && $pageSize > 0) {
            $this->alias('A');
            $this->join('uc_goods_category C on A.gc_id = C.gc_id');
            $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
            $this->join('uc_member D on A.member_id = D.member_id');
            if (!empty($map)) $this->where($map);
            $total = $this->count();
            $show_page = tpl_page($total, $pageSize);
            $this->page($page, $pageSize);
        }

        $field = ['A.*', '(A.goods_storage - A.goods_freeze) as show_storage', 'B.type_id', 'B.type_name', 'C.*', 'D.member_name', 'D.member_mobile'];
        $this->alias('A');
        $this->join('uc_goods_category C on A.gc_id = C.gc_id');
        $this->join('uc_goods_type B on A.goods_typeid = B.type_id');
        $this->join('uc_member D on A.member_id = D.member_id');
        $this->field($field);
        $order = empty($order) ? 'A.goods_id desc' : $order;
        $this->order($order);
        if(!empty($map)) $this->where($map);
        $data = $this->select();

        if( !empty($data) ){
            foreach($data as $k => $v){
                if( !empty($v['gc_relation']) ){
                    $data[$k] = D('GoodsCategory')->getRelation($data[$k]);;
                }
            }
            return $data;
        }

        return $data;
    }

    //修改游戏
    public  function edit(){
        if ( IS_POST ) {
            $this->create();
            $result = $this->save();
        }
        return $result;
    }

    //商品库存操作， 冻结(freeze)/释放(release)/扣除(reduce)
    public function changeStorage($goods_id, $amount, $action = 'freeze'){
        if( (int)$goods_id <= 0 || (int)$amount <= 0 ) return false;
        $goods_id = (int)$goods_id;
        $amount = (int)$amount;
        $map['goods_id'] = $goods_id;

        switch($action){
            case 'freeze':
                // 冻结库存
                $data['goods_freeze'] = array('exp', 'goods_freeze + ' . $amount);
                return $this->where($map)->save($data);
                break;
            case 'release':
                // 释放冻结库存
                $data['goods_freeze'] = array('exp', 'goods_freeze - ' . $amount);
                return $this->where($map)->save($data);
                break;
            case 'reduce':
                // 真正扣除库存
                $data['goods_freeze'] = array('exp', 'goods_freeze - ' . $amount);
                $data['goods_storage'] = array('exp', 'goods_storage - ' . $amount);
                return $this->where($map)->save($data);
                break;
            default:
                return false;
        }
    }

}